package com.example.myworkers.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.myworkers.model.Emp;
import com.example.myworkers.service.EmpClockService;
import com.example.myworkers.service.EmpService;
import com.example.myworkers.model.EmpClock;
import com.example.myworkers.dao.EmpClockMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 */
@Service
public class EmpClockServiceImpl extends ServiceImpl<EmpClockMapper, EmpClock>
    implements EmpClockService {

    @Autowired
    private EmpService empService;

    @Override
    public String eClock(EmpClock empClock) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        System.out.println(dateFormat.format(date));

        // 通过empno--来查找这个员工
        Emp emp = empService.getById(empClock.getEmpno());
        empClock.setEmpno(empClock.getEmpno());
        empClock.setEmpname(emp.getEmpname());

        // 根据请求参数判断当天是否打过卡
        LambdaQueryWrapper<EmpClock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(empClock.getEmpno() != null, EmpClock::getEmpno, empClock.getEmpno())
                .eq(StringUtils.isNotBlank(empClock.getEmpname()), EmpClock::getEmpname, empClock.getEmpname())
                .eq(StringUtils.isNotBlank(empClock.getLogintime()), EmpClock::getLogintime, empClock.getLogintime())
                .eq(empClock.getTag() != null,EmpClock::getTag,empClock.getTag());
        EmpClock one = getOne(queryWrapper);

        if (one == null) {
            //没有打过，则上班打卡
            empClock.setStarttime(dateFormat.format(date));
            saveOrUpdate(empClock);
            return "上班打卡成功！";
        }else {
            // 打过，则修改标记并打下班卡
            one.setTag(2);
            one.setEndtime(dateFormat.format(date));
            //更新当日打卡信息
            saveOrUpdate(one);
            return "下班打卡成功";
        }
    }
}




